﻿namespace YAF.Controls
{
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using YAF.Classes.Core;

    public class BasePanel : Panel
    {
        public YafContext PageContext
        {
            get
            {
                if (Site != null && Site.DesignMode)
                {
                    return null;
                }

                return YafContext.Current;
            }
        }

        public string GetUniqueID(string prefix)
        {
            if (!String.IsNullOrEmpty(prefix))
            {
                return prefix + Guid.NewGuid().ToString().Substring(0, 5);
            }
            else
            {
                return Guid.NewGuid().ToString().Substring(0, 10);
            }
        }

        public string GetExtendedID(string prefix)
        {
            string createdID = null;

            if (!String.IsNullOrEmpty(ID))
            {
                createdID = ID + "_";
            }

            if (!String.IsNullOrEmpty(prefix))
            {
                createdID += prefix;
            }
            else
            {
                createdID += Guid.NewGuid().ToString().Substring(0, 5);
            }

            return createdID;
        }
    }

    public class BaseControl : Control
    {
        public YafContext PageContext
        {
            get
            {
                if (Site != null && Site.DesignMode)
                {
                    return null;
                }

                return YafContext.Current;
            }
        }

        public string HtmlEncode(object data)
        {
            return HttpContext.Current.Server.HtmlEncode(data.ToString());
        }

        public void RenderAnchor(HtmlTextWriter writer, string href, string cssClass, string innerText)
        {
            writer.WriteBeginTag("a");
            writer.WriteAttribute("href", href);
            if (!String.IsNullOrEmpty(cssClass))
            {
                writer.WriteAttribute("class", cssClass);
            }

            writer.Write(HtmlTextWriter.TagRightChar);
            writer.Write(innerText);
            writer.WriteEndTag("a");
        }

        public string GetUniqueID(string prefix)
        {
            if (!String.IsNullOrEmpty(prefix))
            {
                return prefix + Guid.NewGuid().ToString().Substring(0, 5);
            }
            else
            {
                return Guid.NewGuid().ToString().Substring(0, 10);
            }
        }

        public string GetExtendedID(string prefix)
        {
            string createdID = null;

            if (!String.IsNullOrEmpty(ID))
            {
                createdID = ID + "_";
            }

            if (!String.IsNullOrEmpty(prefix))
            {
                createdID += prefix;
            }
            else
            {
                createdID += Guid.NewGuid().ToString().Substring(0, 5);
            }

            return createdID;
        }

        public void RenderImgTag(HtmlTextWriter writer, string src, string alt, string title)
        {
            writer.WriteBeginTag("img");

            writer.WriteAttribute("src", src);
            writer.WriteAttribute("alt", alt);

            if (!String.IsNullOrEmpty(title))
            {
                writer.WriteAttribute("title", title);
            }

            writer.Write(HtmlTextWriter.SelfClosingChars);
        }

        #region Render Anchor Begin Functions
        public void RenderAnchorBegin(HtmlTextWriter writer, string href)
        {
            RenderAnchorBegin(writer, href, null, null);
        }

        public void RenderAnchorBegin(HtmlTextWriter writer, string href, string cssClass)
        {
            RenderAnchorBegin(writer, href, cssClass, null, null, null);
        }

        public void RenderAnchorBegin(HtmlTextWriter writer, string href, string cssClass, string title)
        {
            RenderAnchorBegin(writer, href, cssClass, title, null, null);
        }

        public void RenderAnchorBegin(HtmlTextWriter writer, string href, string cssClass, string title, string onclick, string id)
        {
            writer.WriteBeginTag("a");
            writer.WriteAttribute("href", href);
            if (!String.IsNullOrEmpty(cssClass))
            {
                writer.WriteAttribute("class", cssClass);
            }

            if (!String.IsNullOrEmpty(title))
            {
                writer.WriteAttribute("title", HtmlEncode(title));
            }

            if (!String.IsNullOrEmpty(onclick))
            {
                writer.WriteAttribute("onclick", onclick);
            }

            if (!String.IsNullOrEmpty(id))
            {
                writer.WriteAttribute("id", id);
            }

            writer.Write(HtmlTextWriter.TagRightChar);
        }
        #endregion
    }
}
